<?php

/**
 * @author: zhangxingning
 * @contact: zhixiao476@gmail.com
 * @file: UploadsController.php
 * @time: 2021/3/9 20:41
 * @desc:
 */


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Validator;
set_time_limit(0);
class UploadsController extends BaseController {

    public $local_path;         // 路径
    private $upload_root_path;
    private $web_root_path;

    public function avatar(Request $request) {
        $validator = Validator::make($request->all(), [
            'file' => 'required|mimes:jpg,png,jpeg,gif'
        ]);

        if ($validator->fails()) {
            $error_msg = $validator->errors()->first();
            return $this->apiError($error_msg, 1);
        }
        $file = $request->file('file');//获取文件
        $fileSize = $file->getSize();
        if ($fileSize > 1024 * 1024 * 10) return $this->apiError('图片文件超过10M不允许上传！', 1);
        $this->upload_root_path = public_path("/uploads/avatar/");
        $this->web_root_path = "/uploads/avatar/";

        $filename = $this->generateFileName($file, $this->upload_root_path);

        try {
            $file->move($this->upload_root_path, $filename);
        } catch (\Exception $e) {
            return $this->apiError($e->getMessage(), 1);
        }
        $data = [
            'src' => env('UPLOAD_DOMAIN').$this->web_root_path.$filename,
        ];

        return $this->apiSuccess($data);
    }

    public function images(Request $request){
        $validator = Validator::make($request->all(), [
            'file' => 'required|mimes:jpg,png,jpeg,gif'
        ]);

        if ($validator->fails()) {
            $error_msg = $validator->errors()->first();
            return $this->apiError($error_msg, 1);
        }
        $file = $request->file('file');//获取文件
        $fileSize = $file->getSize();
        if ($fileSize > 1024 * 1024 * 100) return $this->apiError('图片文件超过100M不允许上传！', 1);
        $this->upload_root_path = public_path("/uploads/");
        $this->web_root_path = "/uploads/";

        $filename = $this->generateFileName($file, $this->upload_root_path);

        try {
            $file->move($this->upload_root_path, $filename);
        } catch (\Exception $e) {
            return $this->apiError($e->getMessage(), 1);
        }
        $data = [
            'src' => env('UPLOAD_DOMAIN').$this->web_root_path.$filename,
        ];

        return $this->apiSuccess($data);
    }

    protected function generateFileName($file, $path) {
        $filename = date("YmdHis")."_".Str::random(5);

        // Make sure the filename does not exist, if it does, just regenerate
        while (file_exists($path . $filename . '.' . $file->getClientOriginalExtension())) {
            $filename = Str::random(20);
        }

        return $filename . '.' . $file->getClientOriginalExtension();
    }

    /**
     * 上传文件
     * @param Request $request
     */
    public function file(Request $request){
        /*$validator = Validator::make($request->all(), [
            'file' => 'required|mimes:video'
        ]);

        if ($validator->fails()) {
            $error_msg = $validator->errors()->first();
            return $this->apiError($error_msg, 1);
        }*/
        $file = $request->file('file');//获取文件

        if ($file->getSize() > 1024 * 1024 * 1000) return $this->apiError('您上传的标题文件超过100M，不允许上传！', 1);

        $this->upload_root_path = public_path("/uploads/");
        $this->web_root_path = "/uploads/";

        $filename = $this->generateFileName($file, $this->upload_root_path);

        try {
            $file->move($this->upload_root_path, $filename);
        } catch (\Exception $e) {
            return $this->apiError($e->getMessage(), 1);
        }
        $data = [
            'src' => env('UPLOAD_DOMAIN').$this->web_root_path.$filename,
        ];
        return $this->apiSuccess($data);
    }

}
